% [X]=basicCompressiveSensing([1,0,2;0,2,2],[1,0,1;0,1,1],2,0.5,10)
function [ X_est ] = basicCompressiveSensing( M,B,r,lambda,t )
%   Solution in paper: A compressive sensing approach to urban traffic
%   estimation with probe vehicles
%   M:m*n measurement matrix
%   B:m*n indicator matrix
%   r:rank bound
%  lambda: tradeoff coefficient
%   t:iteration times
%  ERROR: without B

[m,n] = size(M);
L = rand(m,r);
v_min =inf;
L_est = zeros(m,r);
R_est= zeros(n,r);
for i=1:t
    R = getInverse([L;sqrt(lambda)*eye(r)],[M;zeros(r,n)])';
    L = getInverse([R;sqrt(lambda)*eye(r)],[M';zeros(r,m)])';
    v = norm(B.*(L*R')-M,2)+lambda*(norm(L,2)+norm(R',2));
    
    if v<v_min
        R_est = R;
        L_est = L;
        v_min = v;
    end
end
X_est = L_est * R_est';
end

function C=getInverse(P,Q)
    C = inv(P'*P)*(P'*Q);
end
